/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-05-27
 * Time: 16:10
 */

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;
//         注意这里面的判断条件并不是 是否为 空
//         判断两者是否相等, 保证了他们都直走一遍, 不会陷入死循环
//         因为两者最后走的长度相同, 要么中间遇到相同节点, 要么是最后都走到了空
        while (cur1 != cur2) {
            cur1 = cur1 == null ? pHead2 : cur1.next;
            cur2 = cur2 == null ? pHead1 : cur2.next;
        }
        return cur1;
    }
}
